//
//  BDGeneralCell.m
//  BadBoy
//
//  Created by wen on 2017/9/6.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDGeneralCell.h"

@interface BDGeneralCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation BDGeneralCell

+ (instancetype)cellForTableView:(UITableView *)tableView model:(BDGeneralModel *)model indexPath:(NSIndexPath *)indexPath identifier:(NSString *)identifier{
    NSString *curidentifier = [self getIdentifierWithModel:model identifier:identifier];
    BDGeneralCell *cell = [tableView dequeueReusableCellWithIdentifier:curidentifier];
    if (cell == nil) {
        cell = [[BDGeneralCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:curidentifier];
        cell.generalModel = model;
        [cell _addSubView];
    }
    [cell layoutWithModel:model indexPath:indexPath];
    return cell;
}

+ (NSString *)getIdentifierWithModel:(BDGeneralModel *)model identifier:(NSString *)identifier {
    NSString *restIdentifier = identifier;
    if (model.mode & BDGeneralCellModeDefault) {
        restIdentifier = [restIdentifier stringByAppendingString:@"default"];
    }else{
        if (model.mode & BDGeneralCellModeTextfield) {
            restIdentifier = [restIdentifier stringByAppendingString:@"textfield"];
        }
        if (model.mode & BDGeneralCellModeRightImage) {
            restIdentifier = [restIdentifier stringByAppendingString:@"rightImage"];
        }
        if (model.mode & BDGeneralCellModeSwitch) {
            restIdentifier = [restIdentifier stringByAppendingString:@"switch"];
        }
        if (model.mode & BDGeneralCellModeButton) {
            restIdentifier = [restIdentifier stringByAppendingString:@"button"];
        }
        if (model.mode & BDGeneralCellModeLabel) {
            restIdentifier = [restIdentifier stringByAppendingString:@"label"];
        }
    }
    return restIdentifier;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)_addSubView{
    
    _topLine = [[UIView alloc] init];
    _topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.contentView addSubview:_topLine];
    
    _bottomLine = [[UIView alloc] init];
    _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.contentView addSubview:_bottomLine];
    
    __weak BDGeneralCell *weakSelf = self;
    if (_generalModel.mode & BDGeneralCellModeDefault) {
        
    }else{
        if (_generalModel.mode & BDGeneralCellModeTextfield) {
            _textField = [[UITextField alloc] init];
            [self.contentView addSubview:_textField];
            _textField.delegate = self;
        }
        if (_generalModel.mode & BDGeneralCellModeRightImage) {
            _aImageView = [[UIImageView alloc] init];
            [self.contentView addSubview:_aImageView];
        }
        if (_generalModel.mode & BDGeneralCellModeSwitch) {
            _aSwitch = [[UISwitch alloc] init];
            [self.contentView addSubview:_aSwitch];
            [[_aSwitch rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
                if (_switchBlock) {
                    _switchBlock(weakSelf);
                }
            }];
        }
        if (_generalModel.mode & BDGeneralCellModeButton) {
            _aButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.contentView addSubview:_aButton];
            [[_aButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                if (_clickButtonBlock) {
                    _clickButtonBlock(weakSelf);
                }
            }];
        }
        if (_generalModel.mode & BDGeneralCellModeLabel) {
            _aLabel = [[UILabel alloc] init];
            [self.contentView addSubview:_aLabel];
        }
        if (_generalModel.mode & BDGeneralCellModeCustomView) {
            _customView = [[UIView alloc] init];
            [self.contentView addSubview:_customView];
        }
    }
}

- (void)layoutWithModel:(BDGeneralModel *)model indexPath:(NSIndexPath *)indexPath{
    _generalModel = model;
    _indexPath = indexPath;
    [self _configeWithModel:_generalModel];
}

- (void)_configeWithModel:(BDGeneralModel *)model{
    if (model.image) {
        self.imageView.hidden = NO;
        if ([model.image isKindOfClass:[UIImage class]]) {
            self.imageView.image = model.image;
        }else if ([model.image isKindOfClass:[NSData class]]) {
            self.imageView.image = [[UIImage alloc] initWithData:model.image];
        }else if ([model.image isKindOfClass:[NSString class]]) {
            switch (model.imageType) {
                case BDGeneralCellImageTypeImageName:
                    self.imageView.image = [UIImage imageNamed:model.image];
                    break;
                case BDGeneralCellImageTypeURL:{
                    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image]
                                      placeholderImage:model.placeImage.length ? [UIImage imageNamed:model.placeImage] : nil];
                }
                    break;
                case BDGeneralCellImageTypeBundlePath:
                    self.imageView.image = [[UIImage alloc] initWithContentsOfFile:model.image];
                    break;
                default:
                    break;
            }
        }
    }else{
        self.imageView.hidden = YES;
    }
    if (model.title) {
        self.textLabel.hidden = NO;
        self.textLabel.text = model.title;
    }else{
        self.textLabel.hidden = YES;
    }
    if (model.subTitle) {
        self.detailTextLabel.hidden = NO;
        self.detailTextLabel.text = model.subTitle;
    }else{
        self.detailTextLabel.hidden = YES;
    }
    
    
    if (model.accessoryType == BDGeneralCellAccessoryTypeDisclosureIndicator) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    if (model.hasSeparator == YES) {
        _bottomLine.hidden = NO;
        if (_indexPath.row == 0) {
            _topLine.hidden = NO;
        }else{
            _topLine.hidden = YES;
        }
    }else{
        _topLine.hidden = YES;
        _bottomLine.hidden = YES;
    }
    
    _textField.hidden = YES;
    _aSwitch.hidden = YES;
    _aButton.hidden = YES;
    _aImageView.hidden = YES;
    _aLabel.hidden = YES;
    _customView.hidden = YES;
    if (model.mode & BDGeneralCellModeTextfield) {
        self.textField.hidden= NO;
        _textField.text = model.textFieldText;
        _textField.placeholder = model.placeHolder;
        if (model.keyboardType == BDGeneralCellKeyboardTypeNumberPad) {
            _textField.keyboardType = UIKeyboardTypeNumberPad;
        }else{
            _textField.keyboardType = UIKeyboardTypeDefault;
        }
    }
    
    if (model.mode & BDGeneralCellModeRightImage) {
        _aImageView.hidden = NO;
        _aImageView.image = [UIImage imageNamed:model.aimageString];
    }
    
    if (model.mode & BDGeneralCellModeSwitch) {
        self.aSwitch.hidden = NO;
        [self.aSwitch setOn:model.switchValue];
    }
    if (model.mode & BDGeneralCellModeButton) {
        self.aButton.hidden = NO;
    }
    if (model.mode & BDGeneralCellModeLabel) {
        self.aLabel.hidden = NO;
        self.aLabel.text = model.laberString;
    }
    if (model.mode & BDGeneralCellModeCustomView) {
        self.customView.hidden = NO;
    }
}

- (void)_layout{
    __weak BDGeneralCell *weakSelf = self;
    if (_subviewBlock) {
        _subviewBlock(weakSelf);
    }
    _topLine.width = self.width;
    _topLine.height = 0.75;
    _topLine.top = 0;
    _topLine.left = 0;
    
    _bottomLine.width = self.width;
    _bottomLine.height = 0.75;
    _bottomLine.bottom = self.height;
    _bottomLine.left = 0;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self _layout];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (_editTextFieldBlock) {
        _editTextFieldBlock(self);
    }
}

@end
